package com.irdstudio.tdp.executor.core.plugin.apps;

import com.irdstudio.sdk.beans.core.util.KeyUtil;
import com.irdstudio.tdp.executor.core.plugin.AbstractPlugin;
import com.irdstudio.tdp.executor.core.plugin.gencode.dao.PaasAppsInfoDao;
import com.irdstudio.tdp.executor.core.plugin.gencode.dao.PaasDeployInfoDao;
import com.irdstudio.tdp.executor.core.plugin.gencode.vo.PaasAppsInfo;
import com.irdstudio.tdp.executor.core.plugin.gencode.vo.PaasDeployInfo;
import com.irdstudio.tdp.executor.core.util.date.CurrentDateUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 初始化应用服务器分配信息插件
 * @author ligm
 * @date 2019-10-14
 */
public class AppServerAllotPlugin extends AbstractPlugin {
	
	/** 应用信息 */
	private PaasAppsInfo appInfo = null;
	
	/** 读取插件所需要参数 */
	@Override
	protected boolean doReadConfigureFromDB(Connection conn, String szConfIdentify) throws SQLException {
		// 根据批次流水号读取应用信息
		String batchSerialNo = context.getSzBatchSn();
		PaasAppsInfoDao paasAppsInfoDao = new PaasAppsInfoDao(conn);
		this.appInfo = paasAppsInfoDao.queryByBatchSerialNo(batchSerialNo);
		return true;
	}

	@Override
	public boolean execute() {
		boolean flag = true;
		Connection conn = null;
		logger.info("调用应用部署服务器分配插件...");
		try {
			conn = this.getPluginConnection();
			// 初始化应用部署信息
			PaasDeployInfoDao adis = new PaasDeployInfoDao(conn);
			// 根据应用标识判断是否已分配，若已分配，则删除原分配信息
			Integer port = checkAndDelPaasDeployInfo(appInfo.getAppId(), adis);

			// 获取 分配的服务器ip
			String ip = "47.107.173.118";
			if (port == -1) {
				PaasDeployInfo param = new PaasDeployInfo();
				param.setEcsIp(ip);
				port = adis.queryMaxAppPort(param);
				port ++ ;
			}
			PaasDeployInfo adiv = new PaasDeployInfo();
			adiv.setAppDeployId(KeyUtil.createUUIDKey());
			adiv.setAppId(appInfo.getAppId());
			adiv.setAppName(appInfo.getAppCode());
			adiv.setAppCnname(appInfo.getAppName());
			adiv.setAppVersion("0.0.1");
			adiv.setAppType("202");
			adiv.setAppOrder(0);
			adiv.setEcsId("AFCD573267114F858443B0011D5BA8E8");
			adiv.setEcsIp(ip);
			adiv.setEcsName("esaas-api-app2");
			adiv.setEcsRegion("aliyun");
			adiv.setAppPort(String.valueOf(port));
			adiv.setAppState("3");
			adiv.setOperUserid("admin");
			adiv.setOperTime(CurrentDateUtil.getTodayDateEx2());
			adis.insertPaasDeployInfo(adiv);
		} catch (Exception e) {
			flag = false;
		} finally {
			if (conn != null) {
				this.closePluginConnection(conn);
			}
		}
		
		return flag;
	}

	private Integer checkAndDelPaasDeployInfo(String appId, PaasDeployInfoDao paasDeployInfoDao) throws SQLException {
		logger.info("根据应用标识: " + appId + "，查询应用部署信息开始...");
		PaasDeployInfo paasDeployInfoVO = new PaasDeployInfo();
		paasDeployInfoVO.setAppId(appId);
		List<PaasDeployInfo> result = paasDeployInfoDao.queryAllByCondition(paasDeployInfoVO);
		if(Objects.nonNull(result) && !result.isEmpty()){
			List<String> appIdList = result.stream().map(PaasDeployInfo::getAppId).collect(Collectors.toList());
			int num = paasDeployInfoDao.batchDeleteByAppIds(appIdList);
			logger.info("删除的应用部署信息为：" + num);
			return Integer.valueOf(result.get(0).getAppPort());
		}else{
			logger.info("根据应用标识: " + appId + "，查询应用部署信息结束，结果为空.");
			return -1;
		}
	}

}
